package com.dating.base.service;

import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.impl.sql.callback.QueryEntityCallback;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;

import com.dating.base.bean.Permission;
import com.dating.base.bean.Role;
import com.dating.base.bean.User.Type;
import com.dating.base.dao.PermissionDao;
import com.dating.base.dto.Pager;

@IocBean
public class PermissionService{

	@Inject
	private Dao dao ;
	
	@Inject
	private PermissionDao permissionDao ;
	
	
	/**
	 * 根据id查找权限
	 * @param id
	 * @return
	 */
	public Permission findById(Integer id){
		return permissionDao.findById(id);
	}
	
	/**
	 * 根据name查询权限
	 * @param name
	 * @return
	 */
	public Permission findByName(String name){
		return permissionDao.findByCondition(Cnd.where("name","=",name));
	}
	
	/**
	 * 查找所有权限
	 * @return
	 */
	public List<Permission> findAll(){
		return permissionDao.findAll();
	}
	
	/**
	 * 分页查询权限
	 * @param pager
	 * @return
	 */
	public QueryResult findAllByPage(Pager pager){
		List<Permission> list = dao.query(Permission.class, null, pager);
		pager.setRecordCount(dao.count(Permission.class));
		return new QueryResult(list, pager);
	}
	
	
	/**
	 * 添加权限
	 * @param permission
	 */
	public void save(Permission permission){
		permissionDao.save(permission);
	}
	
	/**
	 * 批量删除权限
	 * @param ids
	 * @return
	 */
	public int batchDel(String ids){
		return dao.clear(Permission.class, Cnd.where("id" , "in", ids.split("\\,")).and("installed", "=",false));
	}
	
	/**
	 * 更新权限
	 * @param perm
	 * @return
	 */
	public boolean update(Permission perm){
		return permissionDao.update(perm);
	}
	
	
	/**
	 * 更新权限
	 * @param perm
	 * @return
	 */
	public boolean updateIgnoreNull(Permission perm){
		return permissionDao.updateIgnoreNull(perm);
	}
	
	/**
	 * 根据用户id，查询所拥有的权限
	 */
	public List<Permission> findByUser(int userId, Type userType) {
		Sql sql = dao.sqls().create("find.permission.by.user.id");
		sql.params().set("userid", userId);
		sql.params().set("type", userType);
		sql.setEntity(dao.getEntity(Permission.class));
		sql.setCallback(new QueryEntityCallback());
		dao.execute(sql);
		return sql.getList(Permission.class);
	}
	
	
	/**
	 * 根据角色列表查询权限
	 * @param roles
	 * @return
	 */
	public List<Permission> findByRoles(List<Role> roles){
		List<Integer> roleIds = new ArrayList<Integer>();
		for (Role role : roles) {
			roleIds.add(role.getId());
		}
		Sql sql = dao.sqls().create("find.permission.by.role");
		sql.setEntity(dao.getEntity(Permission.class));
		sql.setCondition(Cnd.where("r_id", "in", roleIds.size() == 0 ? "-1" : Lang.concat(",", roleIds)));
		sql.setCallback(new QueryEntityCallback());
		dao.execute(sql);
		return sql.getList(Permission.class);
	}
	
	
	/**
	 * 查找角色拥有的权限
	 * @param roleIds
	 * @return
	 */
	public List<Permission> findByRoles(Integer[] roleIds){
		if(roleIds == null || roleIds.length == 0){
			return new ArrayList<Permission>();
		}
		Sql sql = dao.sqls().create("find.permission.by.role");
		sql.setEntity(dao.getEntity(Permission.class));
		sql.setCondition(Cnd.where("r_id", "in", Lang.concat(",", roleIds)));
		sql.setCallback(new QueryEntityCallback());
		dao.execute(sql);
		return sql.getList(Permission.class);
	}
}
